//
//  RNOLDiscovererViewController.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/8.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLDiscovererViewController.h"
#import "RNOLDiscoverItemModel.h"
#import "RNOLRongNiuOnLineViewController.h"
#import "RNOLActivityCenterViewController.h"

@interface RNOLDiscovererViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSDictionary *sectionDict;

@end


@implementation RNOLDiscovererViewController

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , KHIGHT-kTabBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        adjustsScrollViewInsets_NO(_tableView, self);
//        _tableView.height -= kBottomHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH , 10)];
        header.backgroundColor = kBgColor;
        _tableView.tableHeaderView = header;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview: _tableView];
    }
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBarTitleViewWithText:@"发现"];
    self.view.backgroundColor = KWhiteColor ;
    [self initDataSource];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [RNOLLoginTool setNavgationBarNeedAnimation:NO];

}

- (void)initDataSource{
    [RNOLDiscoverItemModel getLocalPlistData:^(NSDictionary *dic) {
         self.sectionDict = dic ;
        [self.tableView reloadData];
    }];
}

#pragma mark -  UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.sectionDict.allKeys count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *items = self.sectionDict[@(section).stringValue];
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    NSDictionary *dic = (NSDictionary *)self.sectionDict[@(indexPath.section).stringValue][indexPath.row];
    RNOLDiscoverItemModel *model = [RNOLDiscoverItemModel yy_modelWithDictionary: dic];
    cell.accessoryType =  model.haveRightIndicator? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
    cell.imageView.image = [UIImage imageNamed: model.icon];
    cell.textLabel.text = model.title ;
    cell.detailTextLabel.text = model.subText;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = (NSDictionary *)self.sectionDict[@(indexPath.section).stringValue][indexPath.row];
    RNOLDiscoverItemModel *model = [RNOLDiscoverItemModel yy_modelWithDictionary: dic];
    NSString *title = model.title;
    //根据标题做事情
    if ([title isEqualToString:@"融牛在线"]) {
        RNOLRongNiuOnLineViewController *rnolVC = [[RNOLRongNiuOnLineViewController alloc] init];
        [self.navigationController pushViewController: rnolVC animated:YES];
    }else if ([title isEqualToString:@"网站公告"]) {

        
    }else if ([title isEqualToString:@"活动中心"]) {
        [self pushPageWithClass:[RNOLActivityCenterViewController class]];
    }else if ([title isEqualToString:@"媒体报道"]) {
        
        
    }else if ([title isEqualToString:@"帮助中心"]) {
        
        
    }else if ([title isEqualToString:@"客服电话"]) {
        kCallPhone(model.subText);
    }
}



@end
